package com.ui_componet.citychoose;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2015/12/2.
 */
public class CityDataModule {

    private Context context;
    private  DbOpenHelper dbOpenHelper;
    private SQLiteDatabase database;
    private static CityDataModule cityDataModule;

    private  Map<String,String> provinces=new HashMap<>();

    private  Map<String,String> citys=new HashMap<>();

    private  Map<String,String> areas=new HashMap<>();


    private ArrayList<String> provinceList = new ArrayList<>();

    private  Map<String, String> getAreas() {
        return areas;
    }

    private  Map<String, String> getCitys() {
        return citys;
    }

    private  Map<String, String> getProvinces() {
        return provinces;
    }


    private ArrayList<String> getProvinceList() {
        return provinceList;
    }

    public DbOpenHelper getDbOpenHelper() {
        return dbOpenHelper;
    }

    public SQLiteDatabase getDatabase() {
        return database;
    }

    public CityDataModule(Context context){
        this.context = context;
        dbOpenHelper = new DbOpenHelper(context);
        database = dbOpenHelper.openDatabase();
    }

    public static CityDataModule getInstance(Context context){
        if(cityDataModule == null){
            synchronized (CityDataModule.class){
                if(cityDataModule == null){
                    cityDataModule = new CityDataModule(context);
                }
            }
        }
        return cityDataModule;
    }


    /**
     * 获取省
     * */
    public static ArrayList<String> getProvinceList(Context context){
        if(getInstance(context).getProvinceList().size() > 0){
            return getInstance(context).getProvinceList();
        }
        Cursor cursor = getInstance(context).getDatabase().query("t_city", null, "SUPERIOR_CITY_CODE=?", new String[]{"0"}, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                String province = cursor.getString(cursor.getColumnIndex("CITY_NAME"));
                String parent = cursor.getString(cursor.getColumnIndex("CITY_CODE"));
                getInstance(context).getProvinces().put(province,parent);
                getInstance(context).getProvinceList().add(province);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return getInstance(context).getProvinceList();
    }

    /**
     * 获取市
     * */
    public static ArrayList<String> getCityList(Context context,String provinceName) {

        final String provinceCode=getInstance(context).getProvinces().get(provinceName);
        ArrayList<String> cityList = new ArrayList<>();

        if (provinceName!=null&&!provinceName.equals("不限")) {
            Cursor curso = getInstance(context).getDatabase().query("t_city", null, "SUPERIOR_CITY_CODE=?", new String[]{provinceCode}, null, null, null);
            if (curso.moveToFirst()) {
                do {
                    String city = curso.getString(curso.getColumnIndex("CITY_NAME"));
                    String parent = curso.getString(curso.getColumnIndex("CITY_CODE"));
                    getInstance(context).getCitys().put(city,parent);
                    cityList.add(city);
                } while (curso.moveToNext());
            }
            curso.close();
        }
        return cityList;
    }
    /**
     * 获取市
     * */
    public ArrayList<String> getCityListNo(String provinceName) {
        final ArrayList<String> cityBeans = new ArrayList<>();
        final String provinceCode=provinces.get(provinceName);
        Cursor curso = database.query("t_city", null, "SUPERIOR_CITY_CODE=?", new String[]{provinceCode}, null, null, null);
        if (curso.moveToFirst()) {
            do {
                String city = curso.getString(curso.getColumnIndex("CITY_NAME"));
                String parent = curso.getString(curso.getColumnIndex("CITY_CODE"));
                citys.put(city,parent);
                cityBeans .add(city);
            } while (curso.moveToNext());
        }
        curso.close();
        return cityBeans;
    }
    /**
     * 获取区
     * */
    public ArrayList<String> getAreaList(String cityName) {
        final ArrayList<String> cityBeans = new ArrayList<>();
        final String cityCode=citys.get(cityName);

        if (cityName!=null&&!cityName.equals("不限")) {
            Cursor curso = database.query("t_city", null, "SUPERIOR_CITY_CODE=?", new String[]{cityCode}, null, null, null);
            if (curso.moveToFirst()) {
                do {
                    String city = curso.getString(curso.getColumnIndex("CITY_NAME"));
                    String parent = curso.getString(curso.getColumnIndex("CITY_CODE"));
                    areas.put(city,parent);
                    cityBeans .add(city);
                } while (curso.moveToNext());
            }
            curso.close();
        }
        return cityBeans;
    }
    /**
     * 获取区
     * */
    public static ArrayList<String> getAreaListNo(Context context,String cityName) {
        final ArrayList<String> cityBeans = new ArrayList<>();
        final String cityCode=getInstance(context).getCitys().get(cityName);
        Cursor curso = getInstance(context).getDatabase().query("t_city", null, "SUPERIOR_CITY_CODE=?", new String[]{cityCode}, null, null, null);
        if (curso.moveToFirst()) {
            do {
                String city = curso.getString(curso.getColumnIndex("CITY_NAME"));
                String parent = curso.getString(curso.getColumnIndex("CITY_CODE"));
                getInstance(context).getAreas().put(city,parent);
                cityBeans .add(city);
            } while (curso.moveToNext());
        }
        curso.close();
        return cityBeans;
    }

    /**
     * 获取地区码
     * */
    public static   String getAreaCode(Context context,String proviceName,String cityName,String areaName) {
        String areaCode=null;
        if (proviceName.equals("不限")) {
            areaCode="不限";
        }else if (!proviceName.equals("不限")&&cityName.equals("不限")) {
            areaCode=getInstance(context).getProvinces().get(proviceName);
        }else if (!proviceName.equals("不限")&&!cityName.equals("不限")&&areaName.equals("不限")) {
            areaCode=getInstance(context).getCitys().get(cityName);
        }else {
            areaCode=getInstance(context).getAreas().get(areaName);
        }
        return areaCode;
    }
    /**
     * 获取地区码
     * */
    public static   String getAreaCode(Context context,String areaName) {
        return getInstance(context).getAreas().get(areaName);
    }

    /**
     * 根据cityCode查询省市区在列表中的位置
     * 调用mDataUtil.getIndex("431281");
     * 17,11,1２定位湖南 怀化、洪江
     * */
    public static List<Integer> getIndex(Context context,String cityCode){
        List<Integer>  defaultIndex=new ArrayList<>();
        if (cityCode != null&&cityCode.length()==6) {
            String pro=cityCode.substring(0,2);
            int proDefaultIndex=getData(context,pro,"0");
            defaultIndex.add(proDefaultIndex);
            String city=cityCode.substring(0,4);
            int cityDefaultIndex=getData(context,city,pro);
            defaultIndex.add(cityDefaultIndex);
            String area=cityCode;
            int areaDefaultIndex=getData(context,area,city);
            defaultIndex.add(areaDefaultIndex);
        }
        return defaultIndex;
    }

    /**
     * 得到当前标
     * */
    private static int getData(Context context,String pro,String supCode) {
        int index=0;
        ArrayList<String> codes=getListCode(context,supCode);
        if (codes != null&&codes.size()!=0) {
            for (int i = 0; i <codes.size() ; i++) {
                if (codes.get(i).equals(pro)) {
                    index=i;
                }
            }
        }
        return index;
    }

    /**
     * 获取Code列表
     * */
    private static ArrayList<String> getListCode(Context context,String cityCode) {
        final ArrayList<String> cityCodes = new ArrayList<>();
        Cursor curso = getInstance(context).getDatabase().query("t_city", null, "SUPERIOR_CITY_CODE=?", new String[]{cityCode}, null, null, null);
        if (curso.moveToFirst()) {
            do {
                String city = curso.getString(curso.getColumnIndex("CITY_CODE"));
                cityCodes .add(city);
            } while (curso.moveToNext());
        }
        curso.close();
        return cityCodes;
    }



    public static String  saveImgToSd(Context context,int img){
        final  String ALBUM_PATH = Environment.getExternalStorageDirectory() + "/img/";
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), img);
        File dirFile = new File(ALBUM_PATH);
        if(!dirFile.exists()){
            dirFile.mkdir();
        }
        File myCaptureFile = new File(ALBUM_PATH + "imge.png");
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.flush();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return myCaptureFile.getPath();
    }

    public void releaseResource(){
        cityDataModule = null;
    }

}
